#导入flask
from flask import Blueprint,request,json,jsonify

#导入数据库
from db import db1

#导入ACL
from utils.acl import acl

#导入JWT
from utils.jwt import myjwt

#导入redis
from utils.rs import red

#实例化蓝图
bp_user=Blueprint('user',__name__)



#登录操作
@bp_user.route('/login',methods=['POST'])
def login():
    if request.method=='POST':
        #获取前端传递的信息
        data=request.data
        if data:
            data=json.loads(data)
        else:
            data=request.form
        #判断用户名密码是否正确
        sql="select * from users where name='%s' and password='%s'"%(data['name'],data['password'])
        res=db1.find(sql)
        #生成token
        data={'id':res['id'],'name':res['name']}
        token=myjwt.jwt_encode(data)
        #获取用户页面权限、接口权限
        pagelist=acl.get_interface_reslist(res['id'])
        interfacelist=acl.get_interface_reslist(res['id'])
        print(interfacelist)
        key='interface'+str(res['id'])
        red.insert_set(key,interfacelist)
        if res:
            return jsonify({'code':200,'id':res['id'],'name':res['name'],'token':str(token),'pagelist':pagelist})
        else:
            return jsonify({'code':20010,'mes':'用户名或密码错误'})





